package Phase2;

import java.sql.*;

public class DBDriver extends Phase2 {

    static Connection conn = null;
    static Statement stmt;
    static ResultSet rs;
    public static int accnum = 0;
    public static int max_segs = 0;

    // load a driver
    static void loadDriver() throws InstantiationException, IllegalAccessException {
        try {
            //step 2: Define connection URL
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }
    }

    // make a connection  step 3: establish connection
    static void makeConnection() {
        //for how to set up data source name see below.
        String username = "root";
        String password = "111";
        String url = "jdbc:mysql://localhost:3306/mysql";
        try {
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("Database Connection Successfully Established");
        } catch (SQLException ex) {
            System.err.println("database connection: " + ex.getMessage());
        }
    }

    //create a table
    static void createTable(String createString) {

        // actually pass the user input as a parameter and store it in String
        try {
            //step 4: create a statement
            stmt = conn.createStatement();
            stmt.execute(createString);
            //step 5: execute a query or update.
            //stmt.execute("drop table phase1");//if exists, drop it, get new one
            stmt.executeUpdate(createString);

        } catch (SQLException ex) {
            System.err.println("CreateTable: " + ex.getMessage());
        }
    }

    //insert data to table phase1
    static void insertData(String insertString) {
        try {
            stmt.executeUpdate(insertString);

        } catch (SQLException ex) {
            System.err.println("InsertData: " + ex.getMessage());
        }
    }

    //retrieve data from table Phase1
    public static Phase2 Calculate_Vector_Array() throws SQLException {

        stmt = conn.createStatement();
        stmt.execute("SELECT count(distinct accnum) as accnum_count from phase2");
        rs = stmt.getResultSet();
        while (rs.next()) {
            accnum = rs.getInt("accnum_count");
        System.out.println("Document Count : " + accnum);
        }

        stmt.execute("SELECT MAX(seg) as max_seg from phase2");
        rs = stmt.getResultSet();
        while (rs.next()) {
            max_segs = rs.getInt("max_seg");
        System.out.println("Maximum Segment : " + max_segs);
        }
        Phase2 p1 = new Phase2(accnum, max_segs);
        return (p1);
    }

    static void retrieveData(String searchString) {
        try {

            stmt = conn.createStatement();

            stmt.execute("SELECT accnum,seg,MATCH (txt) AGAINST ('" + searchString + "') AS relevance from phase2 order by accnum,seg");
            rs = stmt.getResultSet();

            Vector_class v = new Vector_class();
            Vector_class v1[] = new Vector_class[accnum];

            for (int i = 0; i < v1.length; i++) {
             v1[i] = new Vector_class(accnum, max_segs);
            }
            v.Initialize_Vector_size(v1, max_segs);
            
            int i = 0;
            int temp = 1;
            System.out.println("accnum        seg        relevance");

            while (rs.next()) {

                
                int s1 = rs.getInt("accnum");
                if (temp != s1) {
                    i++;
                }
                try{
                        v1[i].document_id = s1;
                }catch(ArrayIndexOutOfBoundsException e){
                    e.printStackTrace();
                }
                int n = rs.getInt("seg");
                v1[i].seg_number.add(n);
                float s2 = rs.getFloat("relevance");
                v1[i].relevance.add(s2);
                //System.out.println(s1 + "	" + n + "       " + s2);
                temp = s1;

            }

            v.Process_Vectors(v1);

        } catch (SQLException ex) {
            System.err.println("RetrieveData: " + ex.getMessage());
        }
    }

    static void closeAll() {
        try {
            stmt.close();
            conn.close();
        } catch (SQLException ex) {
            System.err.println("closeAll: " + ex.getMessage());
        }
    }
}

